﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Chess
{
    public class Game
    {
        public static Player turn, whitePlayer, blackPlayer;
        public static Moves Undo = new Moves();
        public static Moves Redo = new Moves();
        public static string filename = "";
       
        static Game()
        {
            whitePlayer = new WhitePlayer();
            blackPlayer = new BlackPlayer();
            turn = whitePlayer;
        }
   
        public static Player isPlayer
        {
            get {return turn;}
            set {turn = value;}
        }

        public static Moves History
        {
            get {return Undo;}
        }

        public static Player WhitePlayer
        {
            get {return whitePlayer;}
        }

        public static Player BlackPlayer
        {
            get {return blackPlayer;}
        }

        public static string FileName
        {
            get { return filename == "" ? "New Game" : filename; }
        }

        public static Move MakeMove(Piece piece, Square square)
        {
            Redo.Clear();
            Move move = piece.Move(square);
            turn = turn.Other;
            return move;
        }
       
        public static void New()
        {
            UndoAllMoves();
            Redo.Clear();
            filename = "";
        }

        public static void Open(string FileName)
        {
            Redo.Clear();
            New();
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(FileName);
            XmlNodeList xmlnodelist;
            xmlnodelist = xmldoc.SelectNodes("/Game/Move");
            foreach (XmlElement xmlnode in xmlnodelist)
                MakeMove(Board.GetPiece(Convert.ToInt32(xmlnode.GetAttribute("FromFile")), Convert.ToInt32(xmlnode.GetAttribute("FromRank"))), Board.GetSquare(Convert.ToInt32(xmlnode.GetAttribute("ToFile")), Convert.ToInt32(xmlnode.GetAttribute("ToRank"))));
            filename = FileName;
        }

        public static void Save(string FileName)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlElement xmlnodeGame = xmldoc.CreateElement("Game");
            xmldoc.AppendChild(xmlnodeGame);
            XmlElement xmlnodeMove;

            foreach (Move move in Undo)
            {
                xmlnodeMove = xmldoc.CreateElement("Move");
                xmlnodeGame.AppendChild(xmlnodeMove);
                xmlnodeMove.SetAttribute("FromRank", move.From.Rank.ToString());
                xmlnodeMove.SetAttribute("FromFile", move.From.File.ToString());
                xmlnodeMove.SetAttribute("ToRank", move.To.Rank.ToString());
                xmlnodeMove.SetAttribute("ToFile", move.To.File.ToString());
              //  xmlnodeMove.SetAttribute("SecondsElapsed", (Convert.ToInt32(move.TimeStamp.TotalSeconds)).ToString());
            }

            xmldoc.Save(FileName);
            filename = FileName;
		
        }

        public static void UndoMove()
        {
            if (Undo.MoveNumber > 0)
            {
                Move undo = Undo.Item(Undo.MoveNumber - 1);
                Redo.Add(undo);
                Move.Undo(undo);
                turn = turn.Other;

            }
        }

        public static void UndoAllMoves()
        {
            while (Undo.MoveNumber > 0)
            {
                UndoMove();
            }
        }

        public static void RedoMove()
        {
            if (Redo.MoveNumber > 0)
            {
                Move moveRedo = Redo.Item(Redo.MoveNumber - 1);
                moveRedo.Piece.Move(moveRedo.To);
                turn = turn.Other;
                Redo.LastMove();

            }
        }

        public static void RedoAllMoves()
        {
            while (Redo.MoveNumber > 0)
            {
                RedoMove();
            }
        }
    }
}
